//
// Created by dddgzc on 2025/1/24.
//

#include "../include/Camera.h"

Camera::Camera() {
    shakeTimer.setIsOneShot(true);
    shakeTimer.setCallback([&]() {
        isShaking = false;
        reset();
    });
}

const Vector2& Camera::getPosition() const {
    return position;
}

void Camera::reset() {
    position.x = 0;
    position.y = 0;
}

void Camera::onUpdate(int delta) {
    shakeTimer.onUpdate(delta);
    if (isShaking) {
        position.x = (-50 + rand() % 100) / 50.0f * shakingStrength;
        position.y = (-50 + rand() % 100) / 50.0f * shakingStrength;
    }
}

void Camera::shake(float strength, float duration) {
    this->isShaking = true;
    this->shakingStrength = strength;
    this->shakeTimer.setWaitTime(duration);
    this->shakeTimer.resetTime();
}